/**
 * 学员资料读取功能
 */
var clientKey = null;
var studentKey = null;

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 初始化
	if( !empty( clientKey ) )
		request_student( clientKey );

	// 点击表格头，隐藏表体
	$( "#tbl_student" ).find( "tr:first>th" ).click( name_row_click );
	$( "#tbl_student" ).find( "tr:first>th" ).css( { "cursor": "pointer" } );
} );

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

/**
 * 点击学员资料的表头时，隐藏表体
 */
function name_row_click()
{
	var tbl = $( this ).parent().parent();
	var visible = tbl.find( "tr:not(:first)" ).visible();
	var trs = tbl.find( "tr:not(:first)" );

	if( visible )
	{
		trs.hide();
	}
	else
	{
		trs.show();
	}
}

function refresh_student()
{
	if( !empty( clientKey ) )
		request_student( clientKey );
}

function request_student( key )
{
	submitter.post( "student_search.php", { key: key }, function( d )
	{
		if( d.err == 0 )
		{
			$( "#studentlst ul" ).empty();

			var size = parseInt( d[ 'size' ] );
			for( var i = 0; i < size; i++ )
			{
				var v = d[ i ];
				insert_student( v );
			}
		}
		else
		{
			infopad.err( d.msg );
		}
	}, "json" );
}

//--------------------------------------------------------------------------
//
//	学员模块操作
//
//--------------------------------------------------------------------------

/**
 * 根据数据参数添加一条学员数据
 * 
 * @param d 数组
 */
function insert_student( d )
{
	var h = "<li rel=\"" + d.key + "\">";

	h += "<p class='title'><span>" + d.nickname + "</span></p>";
	h += "<p><b>性别</b><span>" + d.sex + "</span></p>";
	h += "<p><b>年龄</b><span>" + d.age + " 岁</span></p>";
	h += "<p><b>生日</b><span>" + ( empty( d.birthday ) ? "<em class='caption'>未填写</em>" : d.birthday )
			+ "</span></p>";
	h += "<p><b>学校</b><span>" + ( d.school == null ? "<em class='caption'>未填写</em>" : d.school )
			+ "</span></p>";
	h += "<p><b>学员特征</b></p>";
	h += "<div class=\"content\">" + d.feature + "</div>";
	h += "</li>";

	$( "#studentlst ul" ).append( h );

	// 根据数据选定已经保存的选择
	if( !empty( studentKey ) && studentKey.indexOf( d.key ) != -1 )
		$( "#studentlst ul li:last" ).attr( "class", "selected" );
}

function empty_student()
{
	$( "#studentlst ul" ).empty();
	$( "#studentlst ul" ).append( "<li>暂无学员资料，不能进行预约。</li>" );
}

/**
 * 点击学员资料的表头时，隐藏表体
 */
function name_row_click()
{
	var tbl = $( this ).parent().parent();

	if( tbl.find( "[rel='st_type']" ).val() == "deleted" )
		return;

	var visible = tbl.find( "tr:not(:first)" ).visible();
	var trs = tbl.find( "tr:not(:first)" );

	if( visible )
	{
		trs.hide();
	}
	else
	{
		trs.show();
	}
}

function defaultVal( val )
{
	if( empty( val ) )
		return "";
	return val;
}
